rm(list = ls())
library(dplyr)
##
## Attaching package: 'dplyr'
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(gapminder)
library(ggplot2)
library(tidyr)
library(formatR)
lifeExp_gdp_df <-
gapminder %>%
group_by(continent, lifeExp, gdpPercap)
ggplot(data = lifeExp_gdp_df, aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(shape = continent)) +
geom_point(aes(color = continent))
ggplot(data = lifeExp_gdp_df, aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(shape = continent)) +
geom_point(aes(color = continent)) + scale_x_log10()
ggplot(data = lifeExp_gdp_df, aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(shape = continent)) +
geom_point(aes(color = continent)) + scale_x_log10() +
geom_smooth(method=lm)
ggplot(data = gapminder, aes(x = continent, y = , color = continent )) + geom_area(stat=“bin”) + facet_grid(lifeExp ~ continent)
…can result in unexpected behavior and will not be allowed in a future version of ggplot2. If you want y to represent counts of cases, use stat=“bin” and don’t map a variable to y. If you want y to represent values in the data, use stat=“identity”. See ?geom_bar for examples. (Defunct; last used in version 0.9.2) Error : Mapping a variable to y and also using stat=“bin”. With stat=“bin”, it will attempt to set the y value to the count of cases in each group. This can result in unexpected behavior and will not be allowed in a future version of ggplot2. If you want y to represent counts of cases, use stat=“bin” and don’t map a #variable to y. If you want y to represent values in the data, use stat=“identity”. See ?geom_bar for examples. (Defunct; last used in version 0.9.2) Error : Mapping a variable to y and also using stat=“bin”. With stat=“bin”, it will attempt to set the y value to the count of cases #in each group. This can result in unexpected behavior and will not be allowed in a future #version of ggplot2. If you want y to represent counts of cases, use stat=“bin” and don’t map a variable to y. If you want y to represent values in the data, use stat=“identity”. See ?geom_bar for examples. (Defunct; last used in version 0.9.2)
ggplot(data = gapminder, aes(x = continent, y = lifeExp, color = continent )) +
geom_boxplot()
gdp_df <-
gapminder %>%
group_by(continent, lifeExp)
ggplot(data =gdp_df, aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(shape = continent)) +
geom_point(aes(color = continent))
ggplot(data = gdp_df, aes(x = continent, y = lifeExp, color = continent)) +
geom_boxplot() +
facet_grid(year~continent)
ggplot(data = gdp_df, aes(x = continent, y = lifeExp, color = continent)) +
geom_boxplot() +
facet_wrap(year~continent)
ggplot(data = gdp_df, aes(x = continent, y = lifeExp, color = continent)) +
geom_area() + facet_wrap(~year)
ggplot(data = gdp_df, aes(x = lifeExp, y = , color = continent, fill = continent)) +
geom_density(color = 'black', alpha = 0.5) + facet_wrap(~year)
ggplot(data = gdp_df, aes(x = continent, y = lifeExp, color = continent)) +
geom_boxplot() + facet_wrap(~year)
ggplot(data = gdp_df, aes(x = continent, y = lifeExp, color = continent)) +
geom_boxplot() +
facet_wrap(~year) +
theme_minimal()
ggplot(data = gdp_df, aes(x = continent, y = lifeExp, color = continent)) +
geom_boxplot() +
facet_wrap(~year) +
theme_minimal() +
scale_fill_discrete()
ggplot(data = gdp_df, aes(x = continent, y = lifeExp, color = continent)) +
geom_boxplot() +
facet_wrap(~year) +
theme(axis.text.x=element_text(angle=50, size=10, vjust=0.5)) +
scale_fill_discrete()
select(gapminder)
## data frame with 0 columns and 1704 rows
tbl_df(gapminder)
## Source: local data frame [1,704 x 6]
##
## country continent year lifeExp pop gdpPercap
## (fctr) (fctr) (dbl) (dbl) (dbl) (dbl)
## 1 Afghanistan Asia 1952 28.801 8425333 779.4453
## 2 Afghanistan Asia 1957 30.332 9240934 820.8530
## 3 Afghanistan Asia 1962 31.997 10267083 853.1007
## 4 Afghanistan Asia 1967 34.020 11537966 836.1971
## 5 Afghanistan Asia 1972 36.088 13079460 739.9811
## 6 Afghanistan Asia 1977 38.438 14880372 786.1134
## 7 Afghanistan Asia 1982 39.854 12881816 978.0114
## 8 Afghanistan Asia 1987 40.822 13867957 852.3959
## 9 Afghanistan Asia 1992 41.674 16317921 649.3414
## 10 Afghanistan Asia 1997 41.763 22227415 635.3414
## .. ... ... ... ... ... ...
group_by(gapminder)
## Source: local data frame [1,704 x 6]
##
## country continent year lifeExp pop gdpPercap
## (fctr) (fctr) (dbl) (dbl) (dbl) (dbl)
## 1 Afghanistan Asia 1952 28.801 8425333 779.4453
## 2 Afghanistan Asia 1957 30.332 9240934 820.8530
## 3 Afghanistan Asia 1962 31.997 10267083 853.1007
## 4 Afghanistan Asia 1967 34.020 11537966 836.1971
## 5 Afghanistan Asia 1972 36.088 13079460 739.9811
## 6 Afghanistan Asia 1977 38.438 14880372 786.1134
## 7 Afghanistan Asia 1982 39.854 12881816 978.0114
## 8 Afghanistan Asia 1987 40.822 13867957 852.3959
## 9 Afghanistan Asia 1992 41.674 16317921 649.3414
## 10 Afghanistan Asia 1997 41.763 22227415 635.3414
## .. ... ... ... ... ... ...
ggplot(data = gapminder, aes(x = lifeExp, y = , fill = continent)) +
geom_density(color = "black", alpha = 0.5)
a_data <- filter(gapminder, continent == "Asia")
ggplot(data = a_data, aes(x = lifeExp, y = , color = continent,
fill = continent)) +
scale_fill_manual(values = c("green")) +
geom_density(color = "black", alpha = 0.5) +
geom_vline(aes(xintercept = mean(lifeExp))) +
theme_minimal() +
ggtitle("Life expectancy in Asia")
mean_lifeExp <-
gapminder %>%
group_by(continent) %>%
summarize(avg = mean(lifeExp))
ggplot(data = gapminder, aes(x = lifeExp, y = , colour = continent,
fill = continent)) +
facet_wrap(~continent) +
geom_density(color = "black", alpha = 0.6) +
geom_vline(data = mean_lifeExp, aes(xintercept = avg))
library(ggplot2)
hw_gapminder <- read.csv('/Users/lyman/Dropbox (Byrnes Lab)/R_projects/Intro to R_Fall 2015/hw_gapminder.csv')
mean-lifeExp <- mean(hw_gapminder$lifeExpe)
mean_lifeExp <- mean(hw_gapminder$lifeExp)
small-set <- hw_gapminder[c(1, 2, 3, 4, 1300:1304), (‘country’, ‘continent’, ‘year’)]
small_set <- hw_gapminder[c(1, 2, 3, 4, 1300:1304), c('country', 'continent', 'year')]
mean-gdp <- mean(hw_gapminder$gdpPercap, na.rm = TRUE)
mean_gdp <- mean(hw_gapminder$gdpPercap, na.rm = TRUE)
max-country <- hw-gapminder\(country [which(hw-gapminder\)lifeExp = max(hw-gapminder$lifeExp))]
max_country <- hw_gapminder$country[which(hw_gapminder$lifeExp == max(hw_gapminder$lifeExp))]